/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config;

import opiam.admin.faare.config.javabeans.PersistenceDescriptionMap;

import org.apache.commons.digester.Digester;

import org.apache.log4j.Logger;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.IOException;
import java.io.InputStream;


/**
 * Persistence configuration loading from the XML file.
 *
 * The XML file must contain the following lines :
 * <!DOCTYPE mapping PUBLIC
 *         "-//OPIAM ADMIN//DTD FAARE MAPPING Configuration 1.0//EN"
 *         "http://www.opiam.org/admin/dtd/faare_mapping.dtd">
 *
 * The second line is registered to the digester (it does not need to be an actual URL).
 *
 */
public final class PersistenceConfig implements ErrorHandler
{
    /** DOCTYPE key. */
    public static final String FAARE_CONFIG_DTD_KEY = "-//OPIAM ADMIN//DTD FAARE MAPPING Configuration 1.0//EN";

    /** DOCTYPE URL. */
    public static final String FAARE_CONFIG_DTD_URL = "/config/core/faare_mapping.dtd";

    /** Log4J. */
    private static Logger _logger = Logger.getLogger(PersistenceConfig.class);

    /** XML Parser. */
    private static Digester _persistenceDigester = null;

    /** Current instance. */
    private static PersistenceConfig _instance = null;

    /** Parser exception. */
    private static SAXParseException _saxParseException = null;

    /**
     * Constructor for ProfilesDigester.
     */
    private PersistenceConfig()
    {
        super();
    }

    /**
     * Parsing error callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void error(SAXParseException saxParseException)
    {
        _logger.debug(" error exception!!!!!!!!!!!!!!!");
        _saxParseException = saxParseException;
    }

    /**
     * Parsing fatal error callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void fatalError(SAXParseException saxParseException)
    {
        _logger.debug("fatalError exception!!!!!!!!!!!!!!!");
        _saxParseException = saxParseException;
    }

    /**
     * Parsing warning callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void warning(SAXParseException saxParseException)
    {
        _logger.debug("warning : " + saxParseException.getMessage());
    }

    /**
     * Digester initialization with DTD fields.
     */
    private static void initProfileDigester()
    {
        _logger.debug("initProfileDigester");

        _saxParseException = null;

        _persistenceDigester = new Digester();

        _persistenceDigester.setErrorHandler(_instance);

        _persistenceDigester.setValidating(true);

        _persistenceDigester.register(FAARE_CONFIG_DTD_KEY,
            PersistenceConfig.class.getResource(FAARE_CONFIG_DTD_URL).toString());

        _persistenceDigester.addObjectCreate("mapping",
            "opiam.admin.faare.config.javabeans.PersistenceDescriptionMap");

        _persistenceDigester.addObjectCreate("mapping/ldapconfig",
            "opiam.admin.faare.config.javabeans.JBLdapConfig");
        _persistenceDigester.addSetNext("mapping/ldapconfig", "setLdapConfig",
            "opiam.admin.faare.config.javabeans.JBLdapConfig");
        _persistenceDigester.addSetProperties("mapping/ldapconfig");

        _persistenceDigester.addObjectCreate("mapping/class",
            "opiam.admin.faare.config.javabeans.JBClassDescriptor");
        _persistenceDigester.addSetNext("mapping/class", "addClass",
            "opiam.admin.faare.config.javabeans.JBClassDescriptor");
        _persistenceDigester.addSetProperties("mapping/class");

        _persistenceDigester.addObjectCreate("mapping/class/map-to",
            "opiam.admin.faare.config.javabeans.JBMapTo");
        _persistenceDigester.addSetNext("mapping/class/map-to", "setMapTo",
            "opiam.admin.faare.config.javabeans.JBMapTo");
        _persistenceDigester.addSetProperties("mapping/class/map-to",
            "ldap-oc", "ldapoc");

        _persistenceDigester.addObjectCreate("mapping/class/field",
            "opiam.admin.faare.config.javabeans.JBFieldDescriptor");
        _persistenceDigester.addSetNext("mapping/class/field", "addField",
            "opiam.admin.faare.config.javabeans.JBFieldDescriptor");
        _persistenceDigester.addSetProperties("mapping/class/field");

        _persistenceDigester.addObjectCreate("mapping/class/field/ldap",
            "opiam.admin.faare.config.javabeans.JBSrcAttrDesc");
        _persistenceDigester.addSetNext("mapping/class/field/ldap",
            "setSrcDesc", "opiam.admin.faare.config.javabeans.JBSrcAttrDesc");
        _persistenceDigester.addSetProperties("mapping/class/field/ldap");
    }

    /**
     * Gets PersistenceConfig instance.
     *
     * @return PersistenceConfig instance
     */
    public static PersistenceConfig getInstance()
    {
        _logger.debug("getInstance");

        if (_instance == null)
        {
            _instance = new PersistenceConfig();
            initProfileDigester();
        }

        return _instance;
    }

    /**
     * Gets persistence config.
     *
     * @param in configuration stream
     *
     * @return persistence config
     *
     * @throws IOException exception while reading stream.
     * @throws SAXException exception while parsing data.
     */
    public PersistenceDescriptionMap readConfig(InputStream in)
        throws IOException, SAXException
    {
        _logger.debug("readConfig");

        PersistenceDescriptionMap result = (PersistenceDescriptionMap) _persistenceDigester.parse(in);

        if (_saxParseException != null)
        {
            _logger.debug("Exception : " + _saxParseException.getMessage());
            throw new SAXException(_saxParseException.getMessage());
        }

        return result;
    }
}
